﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BAL.EstRec.FDVIndicador;

namespace BAL.EstRec.FDVGrid
{
    public class RecursoIndexer : INotifyPropertyChanged
    {
        private Dictionary<string, int> _recursoCantidad;
        private List<string> _changedIndexes;
        public string lastModifiedIndex;
        public Pos _pos;
        protected List<Recurso> recursos;
        public CapFDVIndicador indicador;

        public Pos Pos
        {
            get { return _pos; }
        }

        public int this[string key]
        {
            get
            {
                return this._recursoCantidad[key];
            }
            set
            {

                if (this._recursoCantidad.ContainsKey(key))
                {
                    if (value != this._recursoCantidad[key])
                    {
                        this._recursoCantidad[key] = value;
                        this._changedIndexes.Add(key);
                        this.lastModifiedIndex = key;
                        if (this.PropertyChanged != null)
                        {
                            this.PropertyChanged(this, new PropertyChangedEventArgs(key));
                        }
                    }
                }
                else
                {
                    this._recursoCantidad.Add(key, value);
                    //if (this.PropertyChanged != null)
                    //{
                    //    this.PropertyChanged(this, new PropertyChangedEventArgs(String.Empty));
                    //}
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public RecursoIndexer()
        {
            this._changedIndexes = new List<string>();
            this._recursoCantidad = new Dictionary<string, int>();
            this.lastModifiedIndex = "";
            
        }

        public RecursoIndexer(Pos rf,List<Recurso> recursos,FDVRecursoRule rule,CapFDVIndicador indicador)
        {
            this._changedIndexes = new List<string>();
            this._recursoCantidad = new Dictionary<string, int>();
            this.lastModifiedIndex = "";
            this._pos = rf;
            this.recursos = recursos;
            this.indicador = indicador;
            //this.calculateIndicators();
        }

        public string[] getKeys()
        {
            string[] keys = this._recursoCantidad.Keys.ToArray();
            return keys;
        }

        public List<KeyValuePair<string,int>> ToList()
        {
            return this._recursoCantidad.ToList();
        }

        public void noChangeSet(string key,int value)
        {
            if (this._recursoCantidad.ContainsKey(key))
            {
                if (value != this._recursoCantidad[key])
                {
                    this._recursoCantidad[key] = value;
                }
            }
            else
            {
                this._recursoCantidad.Add(key, value);
            }
        }

        /// <summary>
        /// Calcula los indicadores. Retorna null si ocurre un error en el cálculo
        /// </summary>
        public void calculateIndicators()
        {
            //Visitas sugeridas
            var v = 0;
            var listRec = this.ToList();
            try
            {
                v = (from o in listRec
                     join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                     where o.Value > 0
                     group new { o, p } by new { p.idRecurso } into g
                     select new { Recurso = g.Key, maxVal = g.Max(c => c.p.visitasSugeridas) }).Max(m => m.maxVal);
            }
            catch (Exception ex)
            {
                v = 0;
            }

            // tiempo implementación
            var ti = 0;
            try
            {
                ti = (from o in listRec
                      join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                      group new { o, p } by new { p.idRecurso } into g
                      select new { Recurso = g.Key, sumVal = g.Sum(c => c.p.tiempoImplementacion * c.o.Value) }).Sum(m => m.sumVal);
            }
            catch (Exception)
            {

                ti = 0;
            }


            //N{umero de Inicitivas
            var ni = 0;
            try
            {
                ni = (from o in listRec
                      join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                      where o.Value > 0 && !p.excReglaIniciativa
                      group new { o, p } by new { p.idIniciativa } into g
                      select new { Iniciativa = g.Key }).ToList().Count;
            }
            catch (Exception)
            {

                ni = 0;
            }

            //N{umero de Recusos
            var nr = 0;
            try
            {
                nr = (from o in listRec
                      join p in this.recursos on o.Key.ToString() equals p.idRecurso.ToString()
                      where o.Value > 0 && !p.excReglaMaxRecursos
                      group new { o, p } by new { p.idRecurso } into g
                      select new { Recurso = g.Key }).ToList().Count;
            }
            catch (Exception)
            {

                nr = 0;
            }

            this.indicador.TiempoImplementacion = ti;
            this.indicador.VisitasSugeridas = v;
            this.indicador.NumIniciativas = ni;
            this.indicador.NumRecursos=nr;
           
        }
    }
}
